﻿@{
    ViewBag.Title = "产品分类管理";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<div style="margin: 10px 0;">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="append()">添加</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="save()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()">取消</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="Delete()">删除</a>
</div>
<table id="tg" class="easyui-treegrid" title="产品分类管理" style="width: 900px;">
    <thead>
        <tr>
            <th data-options="field:'name',width:200,editor:'text'">中文名</th>
            <th data-options="field:'nameEn',width:200,editor:'text'">英文名</th>
            <th data-options="field:'description',width:200,editor:'text'">中文描述</th>
            <th data-options="field:'descriptionEn',width:200,editor:'text'">英文描述</th>
            <th data-options="width:100"></th>
        </tr>
    </thead>
</table>
<script type="text/javascript">
    var editingId = undefined;//当前旋转选中的行的Id
    $(function () { loadData(); })

    function getSelected() {
        return $('#tg').treegrid('getSelected');
    }

    function loadData() {
        $("#tg").treegrid({
            iconCls: 'icon-ok',
            rownumbers: true,
            animate: true,
            collapsible: true,
            url: '/Admin/GetProductCatgories',
            idField: 'id',
            treeField: 'name',
            onDblClickRow: function (node) {
                if (editingId == undefined) {
                    editingId = getSelected().id;
                    $('#tg').treegrid('beginEdit', editingId);
                }
                else
                    alert("请先保存");
            }
        });
    }

    function append() {
        if (editingId == null) {
            var Pid = 0;
            var node = $('#tg').treegrid('getSelected');
            if (node != null)
                Pid = node.id;
            $('#tg').treegrid('append', {
                parent: Pid,
                data: [{
                    id: 0,
                    name: '请填写中文名',
                    nameEn: '请填写英文名',
                    description: '请填写中文描述',
                    descriptionEn: '请填写英文描述'
                }]
            })
            editingId = 0;
            $('#tg').treegrid('beginEdit', editingId);
        }
        else {
            alert("请先保存");
        }
    }

    function save() {
        if (editingId != undefined) {
            var t = $('#tg');
            var parent = t.treegrid("getParent", editingId);
            var node = t.treegrid("getEditors", editingId);
            var postData = {
                id: editingId,
                name: node[0].target.val(),
                nameEn: node[1].target.val(),
                description: node[2].target.val(),
                descriptionEn: node[3].target.val(),
                pid: parent == null ? 0 : parent.id
            };
            $.post("/Admin/Category", postData, function (data) {
                if (!data.result)
                    alert("更新出错，请重试")
                else
                    alert(data.msg)
                editingId = undefined;
                loadData();

            }, "Json");

        }
    }

    function Delete() {
        var node = $('#tg').treegrid('getSelected');
        if (confirm("确定要删除分类\"" + node.name + "-" + node.nameEn + "\"吗?")) {
            $.post("/Admin/DeleteCategory/" + node.id, function (data) {
                $.messager.alert('提交结果', data.msg, 'info', function () {
                    loadData();
                    if (data.result != 1)
                        return false;
                });
            });
        }
    }

    function cancel() {
        editingId = undefined;
        loadData();
    }
</script>
